package com.aihuishou.monitor.model;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.filter.SimplePropertyPreFilter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MessageParser {
    public static void traverse(Message message, DefaultMessageTree tree) {
        if (message instanceof DefaultTransaction) {
            DefaultTransaction transaction = (DefaultTransaction) message;
            // 创建一个不包含 childList 的复制实例
            DefaultTransaction copy = transaction.copy();
            tree.getTransactionList().add(copy);

            // 递归遍历 childList
            if (transaction.getChildList() != null) {
                for (Message child : transaction.getChildList()) {
                    traverse(child, tree);
                }
            }
        } else if (message instanceof DefaultEvent) {
            DefaultEvent event = (DefaultEvent) message;
            // 创建一个复制实例
            DefaultEvent copy = event.copy();
            tree.getEventList().add(copy);
        }
    }

    public static String getFormatStr(DefaultMessageTree tree) {
        if (tree.getTransactionList() == null && tree.getEventList() == null) {
            return null;
        }
        SimplePropertyPreFilter fieldFilter = new SimplePropertyPreFilter();
        fieldFilter.getExcludes().add("root");
        return JSON.toJSONString(tree, fieldFilter);
    }

    public static List<String> getFormatStrLimit(DefaultMessageTree tree, int logSize) {
        SimplePropertyPreFilter fieldFilter = new SimplePropertyPreFilter();
        fieldFilter.getExcludes().add("root");
        int size = 0;
        if (tree.getTransactionList().size() > 0) {
            size = size + tree.getTransactionList().size();
        }
        if (tree.getEventList().size() > 0) {
            size = size + tree.getEventList().size();
        }
        if (size > logSize) {
            List<String> rootStrList = new ArrayList<>();

            List<Transaction> transactionList = tree.getTransactionList();
            List<Event> eventList = tree.getEventList();

            int totalSize = transactionList.size();
            for (int i = 0; i < totalSize; i += logSize) {
                int end = Math.min(i + logSize, totalSize);

                List<Transaction> subList = transactionList.subList(i, end);
                tree.setTransactionList(subList);
                tree.setEventList(new ArrayList<>());
                String str = JSON.toJSONString(tree, fieldFilter);
                rootStrList.add(str);
            }

            totalSize = eventList.size();
            for (int i = 0; i < totalSize; i += logSize) {
                int end = Math.min(i + logSize, totalSize);

                List<Event> subList = eventList.subList(i, end);
                tree.setEventList(subList);
                tree.setTransactionList(new ArrayList<>());
                String str = JSON.toJSONString(tree, fieldFilter);
                rootStrList.add(str);
            }
            return rootStrList;
        } else {
            String str = JSON.toJSONString(tree, fieldFilter);
            return Collections.singletonList(str);
        }
    }


    public static String getRootStr(Message root) {
        return JSON.toJSONString(root);
    }

}
